﻿

using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using DHJ.Cost.Models.Dto;

namespace DHJ.Cost.Models
{
    public class PiciAppService : AsyncCrudAppService<Pici, PiciDto, int, GetAllPiciInput>, IPiciAppService
    {
        public PiciAppService(IRepository<Pici, int> repository) : base(repository)
        {
        }

        public async Task<ListResultDto<ComboboxItemDto>> GetPiciCombobox()
        {
            var pici = await Repository.GetAllListAsync();
            var dto = pici.Select(p => new ComboboxItemDto(p.Id.ToString(), p.Name + "(" + p.Quantity + ")")).ToList();
            return new ListResultDto<ComboboxItemDto>(dto);
        }

        protected override IQueryable<Pici> CreateFilteredQuery(GetAllPiciInput input)
        {
            return Repository.GetAll()
                .WhereIf(
                !input.Filter.IsNullOrWhiteSpace(),
                e =>
                    e.Name.Contains(input.Filter) ||
                    e.Model.Contains(input.Filter) ||
                    e.TaskPrefixes.Contains(input.Filter)
            );
        }
    }
}
